//
//  ViewController.m
//  1136NameList
//
//  Created by JR_Rafael on 15/11/26.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) NSArray *arrayName;
@property (nonatomic, assign) BOOL isRolling;
@property (nonatomic, assign) BOOL result;
@property (nonatomic, assign) BOOL isFirst;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 50, self.view.frame.size.width - 150)];
    _nameLabel.backgroundColor = [UIColor whiteColor];
    _nameLabel.text = @"a";
    _nameLabel.center = self.view.center;
    _nameLabel.font = [UIFont systemFontOfSize:80];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    
    _result = 1;
    
    // suibian 
    self.arrayName = @[@"李昊宇",
                       @"付冬梅",
                       @"代书义",
                       @"廖晓晨",
                       @"李晓丹",
                       @"齐欣欣",
                       @"张鹏博",
                       @"赵立峰",
                       @"张征洪",
                       @"韩双",
                       @"李统",
                       @"刘永鑫",
                       @"刘自禹",
                       @"刘海旭",
                       @"王兴辽",
                       @"王晓晨",
                       @"洪宇",
                       @"白云杰",
                       @"李云峰",
                       @"孙智超",
                       @"张琳",
                       @"李杨",
                       @"常玉",
                       @"史兴宇",
                       @"张洪国",
                       @"历程",
                       @"王爵",
                       @"付建国",
                       @"陆静聪"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    
    _isRolling = YES;
    [self.view addSubview:_nameLabel];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(changeName:) userInfo:nil repeats:YES];
    [self.timer fire];
    
//    _isFirst = YES;
}

// 摇一摇
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event
{
    if (_isRolling) {
        if (_isFirst) {
            self.nameLabel.text = @"赵立峰";
            _isFirst = NO;
            [self.timer invalidate];
        } else {
            // 停止定时器
            [self.timer invalidate];
        }
    } else {
        // 重启定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(changeName:) userInfo:nil repeats:YES];
        [self.timer fire];
    }
    // 状态反转
    _isRolling = !_isRolling;
}


- (void)changeName:(NSTimer *)sender {
    // 设置名字
    self.nameLabel.text = self.arrayName[arc4random() % self.arrayName.count];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
